package com.translate.web.api.controller;

import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.basic.service.contract.IBaseService;
import com.controller.base.AbstractCommonController;
import com.param.impl.IdParam;
import com.param.impl.PageParam;
import com.response.vo.ResponseModel;
import com.translate.entity.News;
import com.translate.vo.NewsVO;
import com.translate.web.service.INewsService;

@RestController
@RequestMapping("news")
public class NewsController extends AbstractCommonController<News, PageParam> {
	private INewsService service;

	public NewsController(INewsService service) {
		this.service = service;
	}

	/**
	 * 首页新闻
	 * @return
	 */
	@PostMapping("/getNews")
	public ResponseModel<List<NewsVO>> getExamples() {
		
		List<NewsVO> listFromCache = this.service.getListFromCache(NewsVO.class);
		
		if (0 == listFromCache.size()) {
			return ResponseModel.<List<NewsVO>>success(listFromCache);
		}
		
		
		return ResponseModel.<List<NewsVO>>success(listFromCache);
	}
	
	@PostMapping("/newsById")
	public ResponseModel<NewsVO> newsById(@RequestBody @Validated IdParam param) {
		
		NewsVO articleById = this.service.getArticleById(param.getId(), NewsVO.class);
		
		if (null == articleById) {
			return ResponseModel.<NewsVO>errorMsg(null, "获取失败");
		}
		
		return ResponseModel.<NewsVO>success(articleById);
	}
	


	@Override
	protected IBaseService<News> getBaseService() {
		return this.service;
	}


	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) NewsVO.class;
	}
}